﻿namespace Sai.Core.Rules
{
	using System.Linq;
	using Sai.Core.Introspection;

	/// <summary>
	/// Looking for changes in the interfaces a type implements.
	/// </summary>
    public class InterfacesRemoved : IReferenceTypeRule
    {
        public void Validate(ReferenceTypeDeclaration was, ReferenceTypeDeclaration isNow, ITypeDifference typeDifference)
        {
            var removed = was.Interfaces.Except(isNow.Interfaces).ToList();

            if (removed.Any())
            {
                foreach (var remove in removed)
                {
                    typeDifference.IdentifiedChange(
                        new UnaryTypeChange(
                            TypeChangeReason.TypeInterfaceRemoved,
                            remove,
                            "Interface removed"
                        ));
                }
            }
        }
    }

    public class InterfacesAdded : IReferenceTypeRule
	{
        public void Validate(ReferenceTypeDeclaration was, ReferenceTypeDeclaration isNow, ITypeDifference typeDifference)
		{
            var added = isNow.Interfaces.Except(was.Interfaces).ToList();

            if (added.Any())
            {
                foreach (var add in added)
                {
                    typeDifference.IdentifiedChange(
                        new UnaryTypeChange(
                            TypeChangeReason.TypeInterfaceAdded,
                            add,
                            "Interface added"
                        ));
                }
            }
		}
	}
}
